package com.example.cinema.entity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.cinema.entity.BizExeption;
import com.example.cinema.entity.Studios;
import com.example.cinema.mapper.StudiosMapper;
import com.example.cinema.entity.service.IStudiosService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yc138
 * @since 2024-08-26
 */
@Service
public class StudiosServiceImpl extends ServiceImpl<StudiosMapper, Studios> implements IStudiosService {

    @Autowired
    StudiosMapper studiosMapper;
    @Override
    @Transactional(rollbackFor = BizExeption.class)
    public void addStudios(Studios studios) throws BizExeption {
        LambdaQueryWrapper<Studios> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Studios::getName,studios.getName());
        if (studiosMapper.exists(lqw)) {
            throw new BizExeption("该影院已存在","name");
        }
        studiosMapper.insert(studios);
    }

    @Override
    @Transactional(rollbackFor = BizExeption.class)
    public void updateStudios(Studios studios) throws BizExeption {
        studiosMapper.updateById(studios);
        LambdaQueryWrapper<Studios> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Studios::getName,studios.getName());
        List<Studios> list = studiosMapper.selectList(lqw);
        if(list.size() > 1){
            throw new BizExeption("该影院已存在","name");
        }
    }
}
